//
//  ZYXNSCodingViewController.m
//  OCDataStore
//
//  NSCoding 使用
//

#import "ZYXNSCodingViewController.h"
#import "ZYXNSCodingStudent.h"

@interface ZYXNSCodingViewController ()
- (IBAction)save;
- (IBAction)read;
@end

@implementation ZYXNSCodingViewController

- (IBAction)save {
    // 1.新的模型对象
    ZYXNSCodingStudent *stu = [[ZYXNSCodingStudent alloc] init];
    stu.no = @"329023143";
    stu.age = 24;
    stu.height = 1.70;
    
    // 2.归档模型对象
    // 2.1.获得Documents的全路径
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    ZYXLog(@"%@",doc);
    
    // 2.2.获得文件的全路径
    NSString *path = [doc stringByAppendingPathComponent:@"solozyx.data"];
    // 2.3.将对象归档
    [NSKeyedArchiver archiveRootObject:stu toFile:path];
}

- (IBAction)read {
    // 1.获得Documents的全路径
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 2.获得文件的全路径
    NSString *path = [doc stringByAppendingPathComponent:@"solozyx.data"];
    
    // 3.从文件中读取ZYXNSCodingStudent对象
    ZYXNSCodingStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    ZYXLog(@"%@  %d  %f", stu.no, stu.age, stu.height);
}

//生成的 solozyx.data 数据文件 放到 NSCoding 目录供参考

//2015-04-04 21:19:08.993 OCDataStore[28788:626091] (null)  0  0.000000
//2015-04-04 21:19:22.305 OCDataStore[28788:626091] /Users/zhaoyingxin/Library/Developer/CoreSimulator/Devices/E92F8A62-B239-4ADA-945B-D8028C94FD5C/data/Containers/Data/Application/4C4B419F-7CCA-4AA5-994C-3E03724A97BA/Documents
//2015-04-04 21:19:25.609 OCDataStore[28788:626091] 329023143  24  1.700000


@end
